<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="35%">
    <el-form :model="params" class="demo-form-inline" ref="params" :rules="rules" label-position="top">
      <el-row :gutter="20">
        <el-col :span="24" v-if="flag">
          <el-form-item :label="$t('config.materialType.upName')" prop="parentId">
            <x-select-tree
              v-model="params.parentId"
              ref="selectTree"
              :options="treeData"
              :placeholder="$t('config.materialType.upNamePlaceholder')"
              :defaultProps="treeProps"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.materialType.name')" prop="materialTypeName">
            <el-input
              v-model="params.materialTypeName"
              :placeholder="$t('config.materialType.namePlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.materialType.code')" prop="materialTypeCode">
            <el-input
              v-model="params.materialTypeCode"
              :placeholder="$t('config.materialType.codePlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.materialType.state')" prop="state">
            <x-radio-group :list-data="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"> {{ $t('common.confirm') }} </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiEditMaterialType, apiAddMaterialType } from '../api/materialType'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    props: {
      treeData: {
        type: Array,
        default: () => []
      },
      treeProps: {
        type: Object,
        default: () => ({})
      },
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        loading: false,
        flag: true,
        params: {
          parentId: '0',
          materialTypeName: '',
          materialTypeCode: '',
          state: '1'
        },
        rules: {
          parentId: [{ required: true, message: this.$t('config.materialType.upNamePlaceholder'), trigger: 'blur' }],
          materialTypeName: [
            { required: true, message: this.$t('config.materialType.namePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('config.materialType.lengthTip'), trigger: 'blur' }
          ],
          materialTypeCode: [
            { required: true, message: this.$t('config.materialType.codePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('config.materialType.lengthTip'), trigger: 'blur' }
          ],
          state: [{ required: true, message: this.$t('config.materialType.statePlaceholder'), trigger: 'change' }]
        }
      }
    },
    methods: {
      handleOpen(info, flag = true) {
        this.visible = true
        this.params.parentId = info?.parentId ?? 0
        this.flag = flag && `${info.parentId}` !== '0'
        if (info?.id) {
          this.title = this.$t('config.materialType.editType')
          this.getInfo(info)
          return
        }
        this.title = this.$t('config.materialType.addType')
      },
      getInfo(info) {
        this.$nextTick(() => {
          this.params = Object.assign(this.params, cloneDeep(info))
          this.params.state += ''
        })
      },
      handleSubmit: throttle(function () {
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params,
              parentId: typeof this.params.parentId === 'object' ? this.params.parentId.value : this.params.parentId
            }
            const { code, msg } =
              'id' in this.params ? await apiEditMaterialType(param) : await apiAddMaterialType(param)
            if (code === EnumApiCode.Success) {
              this.$message.success(param.id ? this.$t('common.editSuccess') : this.$t('common.newSuccess'))
              this.$emit('handleSuccess')
              this.handleClose()
            } else {
              this.$message.error(msg)
            }
          } else {
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      }
    }
  }
</script>
